#!/bin/bash

source "$(
  cd "$(dirname "$0")" || exit 1
  pwd
)/../common.sh"

function install_development() {
  sudo yum install -y wget tar zlib-devel -y
  source /etc/profile
}

function download_STAR() {
  echo "# STAR正在下载"
  wget -t 10 -T 20 -c -P "$1"  https://github.com/alexdobin/STAR/archive/2.7.10a.tar.gz
  if [[ $? -eq 0 ]]; then
    echo -e "\033[1;32;1m# STAR 已经下载成功\033[0m"
  else
    echo -e "\033[31m# STAR下载失败,请重新执行脚本\033[0m"
    exit 1
  fi
}

function install_STAR() {
  echo "# STAR 正在安装"
  tar -xzf "$1"/2.7.10a.tar.gz -C "$1" && rm -rf "$1"/2.7.10a.tar.gz
  cd "$1"/STAR-2.7.10a/source
  make CXX=clang++
  echo -e "\033[1;32;1m# STAR 已经安装完毕\033[0m"
}

function write_STAR_module() {
  cat >"$1/STAR-2.7.10a/STAR-2.7.10a_modulefiles" <<EOF
#%Module1.0
conflict STAR
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
  variable modfile
  set modfile_path [file dirname \$modfile]
  return \$modfile_path
}
set pwd [getModulefileDir]
set STAR \$pwd
setenv STAR \$STAR
prepend-path PATH \$STAR/source/
EOF
}

function main() {
  set -e
  is_check_build_install_directory "$1" "$2" "STAR-2.7.10a.sh"
  is_check_build_equal "$1" "$2"
  install_development
  download_STAR "$2"
  install_STAR "$2"
  write_STAR_module "$2"
}

main "$@"
